<template>
<div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="padding-top:10px;">
        <el-form :inline="true" :model="filters" :size="size" ref="filters" label-width="100px">
            <el-form-item prop="username" label="用户名">
                <el-input v-model="filters.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="mobile" label="注册时间">
                <el-date-picker v-model="filters.createTimeEnds" type="datetimerange" range-separator="至" start-placeholder="注册开始时间" end-placeholder="注册结束日期" value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="nickname" label="昵称">
                <el-input v-model="filters.nickname" placeholder="昵称"></el-input>
            </el-form-item>
            <el-form-item prop="mobile" label="手机号">
                <el-input v-model="filters.mobile" placeholder="手机号"></el-input>
            </el-form-item>
            <el-row>
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage(null)" />
                </el-form-item>
                <el-form-item>
                    <kt-button :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
                </el-form-item>
                <el-form-item style="float: right">
                    <el-button-group>
                        <el-tooltip content="刷新" placement="top">
                            <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="列显示" placement="top">
                            <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
                        </el-tooltip>
                        <el-tooltip content="导出" placement="top">
                            <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
                        </el-tooltip>
                    </el-button-group>
                </el-form-item>
            </el-row>
        </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
        <!--表格显示列界面-->
        <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns" @handleFilterColumns="handleFilterColumns"></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table :height="350" :data="pageResult" :columns="filterColumns" :showBatchDelete="false" :showDeleteOperation="false" :showSelection="false" :showCustomOperation="true" :customLabel='text' @findPage="findPage" @handleEdit="handleEdit" @handleCustom="handleDetail" ref="CyTable"></cy-table>
    <!--新增编辑界面-->
    <el-dialog :title="operation?'查看':'编辑'" width="45%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
        <el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size">
            <el-form-item label="ID" prop="id" v-if="false">
                <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="用户ID" prop="id">
                <el-input v-model="dataForm.id" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="用户账号" prop="username">
                <el-input v-model="dataForm.username" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="用户昵称" prop="nickname">
                <el-input v-model="dataForm.nickname" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
                <el-input v-model="dataForm.mobile" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="会员类型" prop="memberTypeName">
                <el-input v-model="dataForm.memberTypeName" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>

            <el-form-item label="会员等级" prop="levelName">
                <el-input v-model="dataForm.levelName" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="注册时间" prop="createTime">
                <el-input v-model="dataForm.createTime" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="生日" prop="birthDate">
                <el-date-picker v-model="dataForm.birthDate" :disabled="disabledBoolean" type="date" placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-select v-model="dataForm.sex" clearable placeholder="性别" :disabled="disabledBoolean">
                    <el-option v-for="item in optionSex" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="dataForm.password" :disabled="disabledBoolean" clearable placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item label="是否拉黑">
                <el-select v-model="dataForm.isBlacklist" clearable :disabled="disabledBoolean">
                    <el-option label="是" :value="0"></el-option>
                    <el-option label="否" :value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="dataForm.remark" :disabled="disabledBoolean" auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align:center">
            <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
            <el-button :size="size" type="primary" :disabled="disabledBoolean" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}
            </el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import CyTable from "@/views/Core/JicTable";
import KtButton from "@/views/Core/KtButton";
import {
    format,
    formatWithSeperator
} from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";

export default {
    components: {
        CyTable,
        KtButton,
        TableColumnFilterDialog
    },
    data() {
        return {
            size: "small",
            text: '查看',
            disabledBoolean: true,
            filters: {
                username: "",
                nickname: "",
                mobile: ""
            },
            optionSex: [{
                id: 0,
                name: '男'
            }, {
                id: 1,
                name: '女'
            }],
            memberTypeList: [],
            columns: [{
                    prop: "id",
                    label: "序号",
                    minWidth: 50
                },
                {
                    prop: "username",
                    label: "用户账号",
                    minWidth: 100
                },
                {
                    prop: "nickname",
                    label: "昵称",
                    minWidth: 100
                },
                {
                    prop: "mobile",
                    label: "手机号码",
                    minWidth: 100
                },
                {
                    prop: "userCity",
                    label: "生日",
                    minWidth: 100,
                    formatter: this.formatterBirthday
                },
                {
                    prop: "password",
                    label: "会员卡号",
                    minWidth: 100
                },
                {
                    prop: "memberTypeName",
                    label: "会员类型",
                    minWidth: 80,
                    formatter: this.formatterMemberTypeName
                },
                {
                    prop: "levelName",
                    label: "会员等级",
                    minWidth: 80
                },
                {
                    prop: "remainder",
                    label: "会员余额",
                    minWidth: 80
                },
                {
                    prop: "integration",
                    label: "会员积分",
                    minWidth: 80
                },
                {
                    prop: "freezeIntegral",
                    label: "冻结积分",
                    minWidth: 80
                },
                {
                    prop: "statusVal",
                    label: "状态",
                    minWidth: 120
                },
                {
                    prop: "createTime",
                    label: "注册时间",
                    minWidth: 120
                },
            ],
            filterColumns: [],
            pageRequest: {
                pageNum: 1,
                pageSize: 10
            },
            pageResult: {},
            operation: false, // true:查看, false:编辑
            editDialogVisible: false, // 新增编辑界面是否显示
            editLoading: false,
            dataFormRules: {

            },
            // 新增编辑界面数据
            dataForm: {
                id: "",
                memberTypeName: "",
                remark: ""
            }
        };
    },
    methods: {
        formatterBirthday(row, column, cellValue, index) {
            if (cellValue == '') {
                return '1991-12-18'
            }
        },
        formatterMemberTypeName(row, column, cellValue, index) {
            if (cellValue == '') {
                return '平台'
            } else if (cellValue == '普通会员') {
                return '淘宝'
            } else {
                return '京东'
            }
        },
        // 获取分页数据
        findPage: function (data) {
            let this_ = this;
            if (this_.filters == undefined || this_.filters == null) {
                this_.filters = {};
            }
            if (data != null && data.start != null) {
                if (data.start != null) {
                    this.pageRequest.pageNum = data.start;
                }
                if (data.limit != null) {
                    this.pageRequest.pageSize = data.limit;
                }
            }
            this_.filters.start = this.pageRequest.pageNum;
            this_.filters.limit = this.pageRequest.pageSize;
            this.utils.request.queryMemberUserList(this_.filters, function (res) {
                if (res.data.rows == null) {
                    res.data.rows = [];
                }
                this_.$refs.CyTable.findPage({
                    content: res.data.rows,
                    total: res.data.total
                });
            });
        },
        //初始化会员等级
        memberTypeLists: function () {
            let this_ = this;
            this.utils.request.queryMemberTypeList({}, function (res) {
                this_.memberTypeList = res;
            });
        },
        beforeAvatarUpload(file) {
            var isJPG = false;
            if (file.type === 'image/jpeg' || file.type === 'image/png') {
                isJPG = true;
            }
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message({
                    message: '上传等级LOGO只能是 JPG 或 PNG 格式!',
                    type: 'error',
                    center: true,
                    offset: 250,
                    duration: 1000
                });
            }
            if (!isLt2M) {
                this.$message({
                    message: '上传等级LOGO大小不能超过 2MB!',
                    type: 'error',
                    center: true,
                    offset: 250,
                    duration: 1000
                });
            }
            return isJPG && isLt2M;
        },
        handleAvatarSuccessCarouselFigurePic(res, file) {
            this.vehicle.vehicle_img = res.data.imgUrl
        },
        // 显示新增界面
        handleAdd: function () {
            this.editDialogVisible = true;
            this.operation = true;
            this.resetForm("dataForm");
        },
        // 显示查看界面
        handleEdit: function (params) {
            this.editDialogVisible = true;
            this.disabledBoolean = false;
            this.operation = false;
            this.$nextTick(function () {
                this.dataForm = Object.assign({}, params.row);
            });
        },
        // 显示编辑界面
        handleDetail: function (params) {
            this.editDialogVisible = true;
            this.disabledBoolean = true;
            this.operation = true;
            this.$nextTick(function () {
                this.dataForm = Object.assign({}, params.row);
            });
        },
        // 编辑
        submitForm: function () {
            this.$refs.dataForm.validate(valid => {
                if (valid) {
                    this.$confirm("确认提交吗？", "提示", {}).then(() => {
                        var this_ = this;
                        this_.dataForm.birthDate = formatWithSeperator(this_.dataForm.birthDate, '-', ':');
                        this.utils.request.editMemberInfo(this.dataForm, function (res) {
                            if (res.code == "0000") {
                                this_.$message({
                                    message: "操作成功",
                                    type: "success"
                                });
                                this_.$refs["dataForm"].resetFields();
                                this_.editDialogVisible = false;
                                this_.findPage(null);
                            } else {
                                this_.$message({
                                    message: "操作失败, " + res.message,
                                    type: "error"
                                });
                            }
                        });
                    });
                }
            });
        },
        // 时间格式化
        dateFormat: function (row, column, cellValue, index) {
            return format(row[column.property]);
        },
        resetForm(formName) {
            if (this.$refs[formName] != undefined) {
                if (formName === "filters") {
                    this.filters = {};
                }
                this.$refs[formName].resetFields();
                this.findPage(null);
            }
        },
        // 处理表格列过滤显示
        displayFilterColumnsDialog: function () {
            this.$refs.tableColumnFilterDialog.setDialogVisible(true);
        },
        // 处理表格列过滤显示
        handleFilterColumns: function (data) {
            this.filterColumns = data.filterColumns;
            this.$refs.tableColumnFilterDialog.setDialogVisible(false);
        },
        // 处理表格列过滤显示
        initColumns: function () {
            var temp = [];

            $.each(this.columns, function (key, val) {
                temp.push(val);
            });
            this.filterColumns = temp;
        },
        //列表下载
        downloadExcel() {
            this.$confirm("确定下载列表文件?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                .then(() => {
                    let filename = "系统参数下载";
                    this.$refs.CyTable.exportData(
                        this.filters,
                        this.filterColumns,
                        filename
                    );
                })
                .catch(() => {});
        }
    },
    mounted() {
        this.initColumns();
        this.memberTypeLists();
    }
};
</script>

<style scoped>
.el-icon-plus {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.el-icon-plus .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px !important;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
